
function search_counties_hide_all(except) {
  $('search_province_counties').childElements().each(function(item) {
      if ($(item.id) != null) {
        $(item.id).hide();
      }
    })
  if (except != "null") {
    $('search_province_'+except+'_county').show();
  }
}

function get_query() {
  return $('search_query_form').value;
}

function set_query(query) {
  $('search_query_form').value = query;
}

function get_province() {
  return $('search_province_form').value;
}

function set_province(province) {
  search_counties_hide_all(province);
  $('search_province_form').value = province;
}

function get_county() {
  province = get_province();
  if (province == "") {
    return "";
  } else {
    county_selector = 'search_province_'+province+'_county_form';
    return $(county_selector).value;
  }
}

function set_county(county) {
  province = get_province();
  if (province == "") {
    return "";
  } else {
    county_selector = 'search_province_'+province+'_county_form';
    return $(county_selector).value = county;
  }
}


function get_category() {
  return $('search_category_form').value;
}

function set_category(category) {
  $('search_category_form').value = category;
}

function build_filter(searchtype,query) {
  //query = get_query();
  province = get_province();
  county = get_county();
  category = get_category();
  filter = "";

  if (province != "") {
    filter += "/" + province;
    if (county != "") {
      filter += "/" + county;
    }
  }

  if (query == "") {
    filter += "/" + "browse";
  }else{
    filter += "/" + searchtype + "/" + query;
  }

  if (category != "") {
    filter += "/" + category;
  }

  return filter;
}

function geo_changed() {
  filter = build_filter("search",get_query());
  go_to_url(filter);
}

function query_changed() {
    query = get_query();
    if (query.length == 0) {
        $('result').hide();
        return;
    }
    filter = build_filter("quicksearch",query);
    update_result(filter);
}

function go_to_category(category,query) {
  set_category(category);
  filter = build_filter("search",query);
  go_to_url(filter);
}

function go_to_province(province) {
  set_province(province);
  geo_changed();
}

function go_to_county(county) {
  set_county(county);
  geo_changed();
}